Fix ProfilerStandard sorting and a profile error
authorTim Starling <tstarling@wikimedia.org>
Fri, 5 Dec 2014 04:45:29 +0000 (15:45 +1100)
committerBryanDavis <bdavis@wikimedia.org>
Thu, 11 Dec 2014 18:14:13 +0000 (18:14 +0000)
* Sort $this->collated even if collateOnly is set. Also I don't think
  arsort works that way.
* Fix a profiling error in every DB query, which was due to the two
  scoped profiling sections being destroyed in the wrong order.

Change-Id: I6af05f37a5c0391acfa80d54ecbca7a08ad81250

includes/db/Database.php
includes/profiler/ProfilerStandard.php

index eb9d9b3..93ce61f 100644 (file)
@@ -1198,7 +1198,13 @@ abstract class DatabaseBase implements IDatabase {
                        $this->reportQueryError( $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
                }
 
-               return $this->resultObject( $ret );
+               $res = $this->resultObject( $ret );
+
+               // Destroy profile sections in the opposite order to their creation
+               $queryProfSection = false;
+               $totalProfSection = false;
+
+               return $res;
        }
 
        /**
index b40519e..4ce88bd 100644 (file)
@@ -345,7 +345,9 @@ class ProfilerStandard extends Profiler {
                $this->close(); // set "-total" entry
 
                if ( $this->collateOnly ) {
-                       return; // already collated as methods exited
+                       // already collated as methods exited
+                       $this->sortCollated();
+                       return;
                }
 
                $this->collated = array();
@@ -400,7 +402,21 @@ class ProfilerStandard extends Profiler {
                }
 
                $this->collated['-overhead-total']['count'] = $profileCount;
-               arsort( $this->collated, SORT_NUMERIC );
+               $this->sortCollated();
+       }
+
+       protected function sortCollated() {
+               uksort( $this->collated, function ( $a, $b ) {
+                       $ca = $this->collated[$a]['real'];
+                       $cb = $this->collated[$b]['real'];
+                       if ( $ca > $cb ) {
+                               return -1;
+                       } elseif ( $cb > $ca ) {
+                               return 1;
+                       } else {
+                               return 0;
+                       }
+               } );
        }
 
        /**